<?php
class vtnsMenu
{
	public static function render()
	{
		sfContext::getInstance()->getConfiguration()->loadHelpers(array("Url"));
		$sf_culture = sfContext::getInstance()->getUser()->getCulture();
		$_html = '<ul class="menu">';
		
		$q = Doctrine_Query::create()
				->from("Slot s")
				->where("s.display_position = 0")
				->addWhere("s.parent_id = 0");
		
		foreach ($q->fetchArray() as $slot)
		{
			$_html .= '<li>';
			$_html .= '<a href="'.url_for('category/view?category_id='.$slot['category_id'].'-'.vtnsSEO::getPermalink(($sf_culture == "vi")?$slot['display_name_vi']:$slot['display_name'])).'" '.((vtnsMenu::checkSub($slot['id'])?'class="parent"':'')).'>'.(($sf_culture == "vi")?$slot['display_name_vi']:$slot['display_name']).'</a>';
			$_html .= vtnsMenu::getSubMenu($slot['id']);
			$_html .= '</li>';
		}
		
		$_html .= '</ul>';
		echo $_html;
	}
	
	public static function getSubMenu($parent_id)
	{
		$sf_culture = sfContext::getInstance()->getUser()->getCulture();
		sfContext::getInstance()->getConfiguration()->loadHelpers(array("Url"));
		$q = Doctrine_Query::create()
				->from("Slot s")
				->where("s.display_position = 0")
				->addWhere("s.parent_id = ?", $parent_id);
		$_html = "";
		if ($q->count() > 0)
		{
			$_html .= '<div><ul>';
			foreach ($q->fetchArray() as $slot)
			{
				$_html .= '<li><a href="'.url_for('category/view?category_id='.$slot['category_id'].'-'.vtnsSEO::getPermalink(($sf_culture == "vi")?$slot['display_name_vi']:$slot['display_name'])).'" '.((vtnsMenu::checkSub($slot['id'])?'class="parent"':'')).'><span>'.(($sf_culture == "vi")?$slot['display_name_vi']:$slot['display_name']).'</span></a>';
				$_html .= vtnsMenu::getSubMenu($slot['id']);
				$_html .= '</li>';
			}
			$_html .= '</ul></div>';
		}
		return $_html;
	}
	
	public static function checkSub($parent_id)
	{
		$q = Doctrine_Query::create()
				->from("Slot s")
				->where("s.display_position = 0")
				->addWhere("s.parent_id = ?", $parent_id);
		return $q->count() > 0;
	}
}
?>